/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.web.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 返回结果工具类
 * 
 * @author jiadao
 * @version $Id: OutDataUtil.java, v 0.1 2013-4-23 下午1:42:12 jiadao Exp $
 */
public class OutDataUtil {

    /** logger */
    private static final Log logger = LogFactory.getLog(OutDataUtil.class);

    /**
     * 返回响应数据
     * 
     * @param httpServletResponse http响应
     * @param data 数据类
     */
    public static void outData(HttpServletResponse httpServletResponse, Object data) {
        outData(httpServletResponse, JSON.toJSONString(data));
    }

    /**
     * 返回响应数据
     * 
     * @param httpServletResponse http响应
     * @param jsonString json字符串
     */
    public static void outData(HttpServletResponse httpServletResponse, String jsonString) {
        try {
            httpServletResponse.setCharacterEncoding("utf-8");
            httpServletResponse.setHeader("Content-type", "text/html;charset=utf-8");
            PrintWriter writer = httpServletResponse.getWriter();
            writer.print(jsonString);
            httpServletResponse.flushBuffer();
            // 无需flush和close，Tomcat会帮我处理
        } catch (IOException e) {
            logger.error("返回数据结果IO异常", e);
        }
    }
}
